<template>
  <div>
    <el-form
      :model="bpmnFormData"
      label-width="80px"
      :rules="rules"
      size="small"
    >
      <el-form-item
        :label="
          bpmnFormData.value.$type === 'bpmn:Process' ? '流程标识' : '节点ID'
        "
        prop="id"
        @change="updateElementTask('id')"
      >
        <el-input v-model="bpmnFormData.value.id" />
      </el-form-item>
      <el-form-item
        :label="
          bpmnFormData.value.$type === 'bpmn:Process' ? '流程名称' : '节点名称'
        "
        prop="name"
      >
        <el-input
          v-model="bpmnFormData.value.name"
          @change="updateElementTask('name')"
        />
      </el-form-item>
      <!-- 流程的基础属性 -->
      <template v-if="bpmnFormData.value.$type === 'bpmn:Process'">
        <el-form-item label="流程分类" prop="processCategory">
          <el-select
            v-model="bpmnFormData.value.processCategory"
            placeholder="请选择流程分类"
            @change="updateElementTask('processCategory')"
          >
            <el-option
              v-for="dict in sys_process_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
      <el-form-item
        v-if="bpmnFormData.value.$type === 'bpmn:SubProcess'"
        label="状态"
      >
        <el-switch
          v-model="bpmnFormData.value.isExpanded"
          active-text="展开"
          inactive-text="折叠"
          @change="updateElementTask('isExpanded')"
        />
      </el-form-item>

      <el-form-item label="节点描述">
        <el-input
          :rows="2"
          type="textarea"
          v-model="bpmnFormData.value.documentationValue"
          @change="updateDocumentation"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, watch, onMounted } from "vue";
import { StrUtil } from "@/utils/StrUtil";
import modelerStore from "@/components/Process/common/global";

const props = defineProps({
  id: {
    type: String,
    required: true,
  },
});

const { proxy } = getCurrentInstance();
const { sys_process_category } = proxy.useDict("sys_process_category");

const rules = reactive({
  id: [{ required: true, message: "节点Id 不能为空", trigger: "blur" }],
  name: [{ required: true, message: "节点名称不能为空", trigger: "blur" }],
});

const bpmnFormData = reactive({});

watch(
  () => props.id,
  (newVal) => {
    if (StrUtil.isNotBlank(newVal)) {
      resetTaskForm();
    }
  },
  { immediate: true }
);

function resetTaskForm() {
  Object.assign(
    bpmnFormData,
    JSON.parse(JSON.stringify(modelerStore.element.businessObject))
  );
  bpmnFormData.value = JSON.parse(
    JSON.stringify(modelerStore.element.businessObject)
  );
  bpmnFormData.value.documentationValue =
    modelerStore.element.businessObject.documentation?.[0]?.text || "";
}

function updateElementTask(key) {
  const taskAttr = {};

  taskAttr[key] = bpmnFormData.value[key] || null;
  modelerStore.modeling.updateProperties(modelerStore.element, taskAttr);
}

function updateDocumentation() {
  const modeler = modelerStore.modeler;
  const moddle = modeler.get("moddle");
  const modeling = modeler.get("modeling");
  const documentation = moddle.create("bpmn:Documentation", {
    text: bpmnFormData.value.documentationValue,
  });

  let extensionElements = modelerStore.element.businessObject.extensionElements;

  if (!extensionElements) {
    extensionElements = moddle.create("bpmn:ExtensionElements", {
      values: [],
    });
  }

  modeling.updateProperties(modelerStore.element, {
    documentation: [documentation],
    extensionElements: extensionElements,
  });

  modeler.get("commandStack").execute("element.updateProperties", {
    element: modelerStore.element,
    properties: {
      documentation: [documentation],
    },
  });
}
</script>

<style lang="scss"></style>
